<template>
  <el-dialog :title="'退货'" :close-on-click-modal="false" :visible.sync="visible" :append-to-body="true">
    <el-table :data="shopList" v-loading="dataListLoading" style="width: 100%;">
      <el-table-column prop="shopname" header-align="center" align="center" label="名称"></el-table-column>

      <el-table-column prop="returnNumber" header-align="center" align="center" label="可退数">
        <template slot-scope="scope">
          <span>{{scope.row.returnNumber}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="salePrice" header-align="center" align="center" label="单价">
        <template slot-scope="scope">
          <span>￥{{scope.row.shopPrice}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="count" header-align="center" align="center" label="退货数量">
        <template slot-scope="scope">
          <el-input-number size="mini" @change="shopChange" v-model="scope.row.count" :step="1" :min="0" :max="scope.row.returnNumber"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" label="退款">
        <template slot-scope="scope">
          <span>{{scope.row.shopPrice * scope.row.count}}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-form :model="dataForm" style="margin-top: 10px;" ref="dataForm" label-width="80px">
      <el-form-item label="备注" prop="remarks">
        <el-input type="textarea" :rows="2" v-model="dataForm.remarks" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>

    <span class="dialog-footer" slot="footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" :disabled="countNumber == 0" @click="dataFormSubmit">{{ $t('Common.Confirm') }}</el-button>
    </span>

  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        dataListLoading: false,
        visible: false,
        dataForm: {
          id: 0,
          remarks: ''
        },
        countNumber: 0,
        shopList: []
      };
    },
    components: {

    },
    methods: {
      init(id) {
        this.visible = true;
        this.dataForm.id = id;
        this.$nextTick(() => {
          this.getInfo()
        });
      },
      getInfo() {
        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinaccounting/purchaseRecordDetails`),
          method: 'get',
          params: this.$http.adornParams({
            accountId: this.dataForm.id
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.shopList = data.list
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      //计算总数量
      shopChange() {
        this.countNumber = 0;
        this.shopList.forEach(item => {
          this.countNumber += item.count
        });
      },
      dataFormSubmit() {
        var countNumber = 0;
        this.shopList.forEach(item => {
          countNumber += item.count
        });

        if (countNumber == 0) {
          this.$message.error("请选择退货数量");
          return false;
        }

        this.$http({
          url: this.$http.adornUrl(`/hotel/thotelfloorroomcheckinaccounting/returnGoods`),
          method: 'post',
          data: this.$http.adornData({
            accountId: this.dataForm.id,
            remarks: this.dataForm.remarks,
            shopList: JSON.stringify(this.shopList)
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            Object.assign(this.$data, this.$options.data());

              

            this.$message({
              message: this.$t('Common.OperationSuccessful'),
              type: 'success',
              duration: 1000,
              onClose: () => {
this.$emit('getDataList');
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });


      }
    }
  };
</script>

<style></style>
